{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "87ddb043-8d97-48c7-887f-6f03dc41eea0",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2022-01-20 14:11:47,022] INFO  [tsapis.py:28] 从数据库中获取股票列表\n"
     ]
    }
   ],
   "source": [
    "# 获取基础数据\n",
    "from comm.tsapis import Tsapi,logger\n",
    "ts=Tsapi()\n",
    "stocks=ts.get_stock_list()\n",
    "df=ts.get_trade_dtl()\n",
    "df.sort_values(by=['ts_code','trade_date'],ascending=True,inplace=True)\n",
    "tp=df[['trade_date','vol','close','ts_code','pct_chg','tag']]\n",
    "# tp2=tp#[tp['ts_code']=='000001.SZ']\n",
    "tp=tp.drop_duplicates()\n",
    "tp3=tp.groupby(['ts_code'])[['vol','close']].rolling(10).mean()   #.as('mv')\n",
    "tp3.columns=['v_ma','ma']\n",
    "tp3 = tp3.reset_index()\n",
    "tp3.drop(columns=['ts_code'],inplace=True)\n",
    "tp3.set_index('level_1',inplace=True)\n",
    "tp3=tp.join(tp3)\n",
    "tp3=tp3.dropna(axis=0,how='any')\n",
    "tp3['涨跌标记']=tp3['pct_chg'].apply(lambda x:1 if x<=-1 else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "0a6e700f-e48c-46b8-b10c-8990537788a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "dataset: DataFrame格式数据集\n",
    "partionby：分组依据字段\n",
    "orderby：排序依据字段\n",
    "asc:是否为升序；1:升序；0:降序\n",
    "return series格式：序号\n",
    "'''\n",
    "def row_number(dataset, partionby, orderby, asc):\n",
    "    return dataset[orderby].groupby(dataset[partionby]).rank(ascending=asc, method='first')\n",
    "tp3['rk']=row_number(tp3,'ts_code','trade_date',False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "3cf674da-4be9-4df3-a27f-b283b8016d8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "tp3=tp3[tp3.rk<=10]\n",
    "# 连续N天下跌1%的 股票列表\n",
    "tp3['连续涨跌']=tp3['涨跌标记'].rolling(3).sum()\n",
    "lx=tp3[(tp3['连续涨跌']>=3) & (tp3.rk<=2)].drop_duplicates(subset=['ts_code'])['ts_code'].tolist()\n",
    "tp3=tp3[tp3.ts_code.isin(lx)]\n",
    "# 大于最近10日平均交易量的2倍视为放量交易\n",
    "tp3['是否放量']= tp3['v_ma']*2<tp3['vol']\n",
    "# 最近有放量的股票列表\n",
    "fl=tp3[(tp3['是否放量']) & (tp3.rk<=3) ].drop_duplicates(subset=['ts_code'])['ts_code'].tolist()\n",
    "tp3=tp3[tp3.ts_code.isin(fl)]\n",
    "rs=tp3.drop_duplicates(subset=['ts_code'])['ts_code'].tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "108591bc-4e69-43dd-bcbd-42ca83519e71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>stock_cd</th>\n",
       "      <th>stock_nm</th>\n",
       "      <th>area</th>\n",
       "      <th>industry</th>\n",
       "      <th>market</th>\n",
       "      <th>beg_dt</th>\n",
       "      <th>crt_dt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4029</th>\n",
       "      <td>603829.SH</td>\n",
       "      <td>603829</td>\n",
       "      <td>洛凯股份</td>\n",
       "      <td>江苏</td>\n",
       "      <td>电气设备</td>\n",
       "      <td>主板</td>\n",
       "      <td>20171017</td>\n",
       "      <td>2022-01-19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4111</th>\n",
       "      <td>603982.SH</td>\n",
       "      <td>603982</td>\n",
       "      <td>泉峰汽车</td>\n",
       "      <td>江苏</td>\n",
       "      <td>汽车配件</td>\n",
       "      <td>主板</td>\n",
       "      <td>20190522</td>\n",
       "      <td>2022-01-19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        ts_code stock_cd stock_nm area industry market    beg_dt      crt_dt\n",
       "4029  603829.SH   603829     洛凯股份   江苏     电气设备     主板  20171017  2022-01-19\n",
       "4111  603982.SH   603982     泉峰汽车   江苏     汽车配件     主板  20190522  2022-01-19"
      ]
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#选股结果\n",
    "stocks[stocks.ts_code.isin(rs)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "e3a418ab-7547-4d48-8b42-3ea53ff4316f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>trade_date</th>\n",
       "      <th>vol</th>\n",
       "      <th>close</th>\n",
       "      <th>ts_code</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>tag</th>\n",
       "      <th>v_ma</th>\n",
       "      <th>ma</th>\n",
       "      <th>涨跌标记</th>\n",
       "      <th>rk</th>\n",
       "      <th>连续涨跌</th>\n",
       "      <th>是否放量</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>368932</th>\n",
       "      <td>20220106</td>\n",
       "      <td>20078.08</td>\n",
       "      <td>12.63</td>\n",
       "      <td>603829.SH</td>\n",
       "      <td>2.5995</td>\n",
       "      <td>2.0</td>\n",
       "      <td>14184.057</td>\n",
       "      <td>12.233</td>\n",
       "      <td>0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>368931</th>\n",
       "      <td>20220107</td>\n",
       "      <td>42508.00</td>\n",
       "      <td>11.92</td>\n",
       "      <td>603829.SH</td>\n",
       "      <td>-5.6215</td>\n",
       "      <td>-6.0</td>\n",
       "      <td>17016.916</td>\n",
       "      <td>12.220</td>\n",
       "      <td>1</td>\n",
       "      <td>9.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>368930</th>\n",
       "      <td>20220110</td>\n",
       "      <td>27906.00</td>\n",
       "      <td>12.01</td>\n",
       "      <td>603829.SH</td>\n",
       "      <td>0.7550</td>\n",
       "      <td>0.0</td>\n",
       "      <td>18061.016</td>\n",
       "      <td>12.218</td>\n",
       "      <td>0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>368929</th>\n",
       "      <td>20220111</td>\n",
       "      <td>21334.00</td>\n",
       "      <td>12.12</td>\n",
       "      <td>603829.SH</td>\n",
       "      <td>0.9159</td>\n",
       "      <td>0.0</td>\n",
       "      <td>19045.516</td>\n",
       "      <td>12.236</td>\n",
       "      <td>0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>368928</th>\n",
       "      <td>20220112</td>\n",
       "      <td>29623.40</td>\n",
       "      <td>13.33</td>\n",
       "      <td>603829.SH</td>\n",
       "      <td>9.9835</td>\n",
       "      <td>8.0</td>\n",
       "      <td>20938.656</td>\n",
       "      <td>12.337</td>\n",
       "      <td>0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>368927</th>\n",
       "      <td>20220113</td>\n",
       "      <td>13827.00</td>\n",
       "      <td>14.66</td>\n",
       "      <td>603829.SH</td>\n",
       "      <td>9.9775</td>\n",
       "      <td>8.0</td>\n",
       "      <td>20869.556</td>\n",
       "      <td>12.583</td>\n",
       "      <td>0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>368926</th>\n",
       "      <td>20220114</td>\n",
       "      <td>156717.05</td>\n",
       "      <td>15.33</td>\n",
       "      <td>603829.SH</td>\n",
       "      <td>4.5703</td>\n",
       "      <td>4.0</td>\n",
       "      <td>35675.261</td>\n",
       "      <td>12.887</td>\n",
       "      <td>0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>368925</th>\n",
       "      <td>20220117</td>\n",
       "      <td>163260.37</td>\n",
       "      <td>14.09</td>\n",
       "      <td>603829.SH</td>\n",
       "      <td>-8.0887</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>50959.790</td>\n",
       "      <td>13.083</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>368924</th>\n",
       "      <td>20220118</td>\n",
       "      <td>116972.13</td>\n",
       "      <td>13.39</td>\n",
       "      <td>603829.SH</td>\n",
       "      <td>-4.9681</td>\n",
       "      <td>-5.0</td>\n",
       "      <td>60966.703</td>\n",
       "      <td>13.179</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>434188</th>\n",
       "      <td>20220119</td>\n",
       "      <td>67465.70</td>\n",
       "      <td>13.22</td>\n",
       "      <td>603829.SH</td>\n",
       "      <td>-1.2696</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>65969.173</td>\n",
       "      <td>13.270</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>376554</th>\n",
       "      <td>20220106</td>\n",
       "      <td>61815.42</td>\n",
       "      <td>37.99</td>\n",
       "      <td>603982.SH</td>\n",
       "      <td>-3.5297</td>\n",
       "      <td>-4.0</td>\n",
       "      <td>50683.853</td>\n",
       "      <td>45.479</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>376553</th>\n",
       "      <td>20220107</td>\n",
       "      <td>53642.96</td>\n",
       "      <td>38.73</td>\n",
       "      <td>603982.SH</td>\n",
       "      <td>1.9479</td>\n",
       "      <td>1.0</td>\n",
       "      <td>49463.320</td>\n",
       "      <td>44.224</td>\n",
       "      <td>0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>376552</th>\n",
       "      <td>20220110</td>\n",
       "      <td>53491.18</td>\n",
       "      <td>40.79</td>\n",
       "      <td>603982.SH</td>\n",
       "      <td>5.3189</td>\n",
       "      <td>5.0</td>\n",
       "      <td>49528.748</td>\n",
       "      <td>43.583</td>\n",
       "      <td>0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>376551</th>\n",
       "      <td>20220111</td>\n",
       "      <td>26311.50</td>\n",
       "      <td>39.15</td>\n",
       "      <td>603982.SH</td>\n",
       "      <td>-4.0206</td>\n",
       "      <td>-5.0</td>\n",
       "      <td>49350.236</td>\n",
       "      <td>42.777</td>\n",
       "      <td>1</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>376550</th>\n",
       "      <td>20220112</td>\n",
       "      <td>48199.60</td>\n",
       "      <td>41.06</td>\n",
       "      <td>603982.SH</td>\n",
       "      <td>4.8787</td>\n",
       "      <td>4.0</td>\n",
       "      <td>51376.057</td>\n",
       "      <td>42.263</td>\n",
       "      <td>0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>376549</th>\n",
       "      <td>20220113</td>\n",
       "      <td>51412.87</td>\n",
       "      <td>41.71</td>\n",
       "      <td>603982.SH</td>\n",
       "      <td>1.5830</td>\n",
       "      <td>1.0</td>\n",
       "      <td>53739.724</td>\n",
       "      <td>41.613</td>\n",
       "      <td>0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>376548</th>\n",
       "      <td>20220114</td>\n",
       "      <td>55352.36</td>\n",
       "      <td>44.12</td>\n",
       "      <td>603982.SH</td>\n",
       "      <td>5.7780</td>\n",
       "      <td>5.0</td>\n",
       "      <td>51141.906</td>\n",
       "      <td>41.368</td>\n",
       "      <td>0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>376547</th>\n",
       "      <td>20220117</td>\n",
       "      <td>8582.00</td>\n",
       "      <td>39.71</td>\n",
       "      <td>603982.SH</td>\n",
       "      <td>-9.9955</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>46763.930</td>\n",
       "      <td>40.639</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>376546</th>\n",
       "      <td>20220118</td>\n",
       "      <td>6321.00</td>\n",
       "      <td>35.74</td>\n",
       "      <td>603982.SH</td>\n",
       "      <td>-9.9975</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>43846.940</td>\n",
       "      <td>39.838</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>434352</th>\n",
       "      <td>20220119</td>\n",
       "      <td>188636.97</td>\n",
       "      <td>33.65</td>\n",
       "      <td>603982.SH</td>\n",
       "      <td>-5.8478</td>\n",
       "      <td>-6.0</td>\n",
       "      <td>55376.586</td>\n",
       "      <td>39.265</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        trade_date        vol  close    ts_code  pct_chg  tag       v_ma  \\\n",
       "368932    20220106   20078.08  12.63  603829.SH   2.5995  2.0  14184.057   \n",
       "368931    20220107   42508.00  11.92  603829.SH  -5.6215 -6.0  17016.916   \n",
       "368930    20220110   27906.00  12.01  603829.SH   0.7550  0.0  18061.016   \n",
       "368929    20220111   21334.00  12.12  603829.SH   0.9159  0.0  19045.516   \n",
       "368928    20220112   29623.40  13.33  603829.SH   9.9835  8.0  20938.656   \n",
       "368927    20220113   13827.00  14.66  603829.SH   9.9775  8.0  20869.556   \n",
       "368926    20220114  156717.05  15.33  603829.SH   4.5703  4.0  35675.261   \n",
       "368925    20220117  163260.37  14.09  603829.SH  -8.0887 -8.0  50959.790   \n",
       "368924    20220118  116972.13  13.39  603829.SH  -4.9681 -5.0  60966.703   \n",
       "434188    20220119   67465.70  13.22  603829.SH  -1.2696 -2.0  65969.173   \n",
       "376554    20220106   61815.42  37.99  603982.SH  -3.5297 -4.0  50683.853   \n",
       "376553    20220107   53642.96  38.73  603982.SH   1.9479  1.0  49463.320   \n",
       "376552    20220110   53491.18  40.79  603982.SH   5.3189  5.0  49528.748   \n",
       "376551    20220111   26311.50  39.15  603982.SH  -4.0206 -5.0  49350.236   \n",
       "376550    20220112   48199.60  41.06  603982.SH   4.8787  4.0  51376.057   \n",
       "376549    20220113   51412.87  41.71  603982.SH   1.5830  1.0  53739.724   \n",
       "376548    20220114   55352.36  44.12  603982.SH   5.7780  5.0  51141.906   \n",
       "376547    20220117    8582.00  39.71  603982.SH  -9.9955 -8.0  46763.930   \n",
       "376546    20220118    6321.00  35.74  603982.SH  -9.9975 -8.0  43846.940   \n",
       "434352    20220119  188636.97  33.65  603982.SH  -5.8478 -6.0  55376.586   \n",
       "\n",
       "            ma  涨跌标记    rk  连续涨跌   是否放量  \n",
       "368932  12.233     0  10.0   1.0  False  \n",
       "368931  12.220     1   9.0   1.0  False  \n",
       "368930  12.218     0   8.0   1.0  False  \n",
       "368929  12.236     0   7.0   1.0  False  \n",
       "368928  12.337     0   6.0   0.0  False  \n",
       "368927  12.583     0   5.0   0.0  False  \n",
       "368926  12.887     0   4.0   0.0   True  \n",
       "368925  13.083     1   3.0   1.0   True  \n",
       "368924  13.179     1   2.0   2.0  False  \n",
       "434188  13.270     1   1.0   3.0  False  \n",
       "376554  45.479     1  10.0   3.0  False  \n",
       "376553  44.224     0   9.0   2.0  False  \n",
       "376552  43.583     0   8.0   1.0  False  \n",
       "376551  42.777     1   7.0   1.0  False  \n",
       "376550  42.263     0   6.0   1.0  False  \n",
       "376549  41.613     0   5.0   1.0  False  \n",
       "376548  41.368     0   4.0   0.0  False  \n",
       "376547  40.639     1   3.0   1.0  False  \n",
       "376546  39.838     1   2.0   2.0  False  \n",
       "434352  39.265     1   1.0   3.0   True  "
      ]
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tp3.head(50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "478e45f2-5383-4bb8-9214-10b61eb19c8b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
